﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Cache
{
    /// <summary>
    /// RedisCache依赖注入
    /// </summary>
    public static class DependencyInjectionExtensions
    {
        /// <summary>
        /// 注入RedisCache
        /// </summary>
        /// <param name="services"></param>
        /// <param name="connection"></param>
        public static void AddRedisCache(this IServiceCollection services, string connection)
        {
            FreeRedisCache.ConfigRedis = _ => connection;
            services.AddSingleton<ICache>(FreeRedisCache.Instance);
            services.AddSingleton<IRedisCache>(FreeRedisCache.Instance);
        }
        /// <summary>
        /// 注入redisCache，connectionString = Configuration["redis"]
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        public static void AddRedisCache(this IServiceCollection services, IConfiguration configuration)
        {
            string redis = configuration["redis"];
            AddRedisCache(services, redis);
        }
    }
}
